package org.szgb.console.ht.meter.web;

import cn.hutool.http.HttpStatus;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.szgb.console.ht.meter.entity.HtMeterReadingDaily;
import org.szgb.console.ht.meter.service.HtMeterReadingDailyService;
import org.szgb.console.sys.user.entity.SysUser;
import org.szgb.console.utils.PageView;
import org.szgb.console.utils.TokenUtil;
import org.szgb.core.utils.AjaxJson;

import java.io.IOException;
import java.time.LocalDate;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "/ht/metter/readingDaily")
@Tag(name = "日常表计记录管理", description = "日常表计记录管理模块")
public class HtMeterReadingDailyController {
    private final HtMeterReadingDailyService htMeterReadingDailyService;

    @Autowired
    public HtMeterReadingDailyController(HtMeterReadingDailyService htMeterReadingDailyService) {
        this.htMeterReadingDailyService = htMeterReadingDailyService;
    }


    @Operation(summary = "获取表计记录任务信息", description = "根据查询参数获取表计记录任务信息，支持分页查询。使用id获取更详细的信息。")
    @PostMapping("/get")
    public AjaxJson get(@RequestBody(required = false) HtMeterReadingDaily.HtMeterReadingDailyFindDto htMeterReadingDailyFindDto, HttpServletRequest request, HttpServletResponse response) {
        if (htMeterReadingDailyFindDto == null) {
            htMeterReadingDailyFindDto = new HtMeterReadingDaily.HtMeterReadingDailyFindDto();
        }
        // 创建一个PageView对象，用于分页显示
        PageView<HtMeterReadingDaily.HtMeterReadingDailyView> pageView = new PageView<>();
        HtMeterReadingDaily htMeterReadingDaily = htMeterReadingDailyFindDto.toHtMeterReadingDaily();
        // 如果实体对象的ID不为空，则直接调用get方法获取单个实体对象
        if (htMeterReadingDaily != null && htMeterReadingDaily.getId() != null && !htMeterReadingDaily.getId().isEmpty()) {
            HtMeterReadingDaily meterReadingDaily = htMeterReadingDailyService.get(htMeterReadingDaily.getId());
            // 如果实体对象为空，则返回错误响应
            if (meterReadingDaily == null) {
                return AjaxJson.error("数据不存在", HttpStatus.HTTP_NOT_FOUND, request, response);
            }
            // 返回成功的响应，并将实体对象放入响应中
            return AjaxJson.success(request, response).put("htMeterReadingDaily", meterReadingDaily.toHtMeterReadingDailyView());
        } else {
            // 设置Page对象的列表属性，包含根据实体对象查询到的数据
            pageView.setList(HtMeterReadingDaily.HtMeterReadingDailyView.getViewListFromList(htMeterReadingDailyService.findList(htMeterReadingDaily)));
            // 设置Page对象的总记录数属性，用于分页显示
            pageView.setCount(htMeterReadingDailyService.getCount(htMeterReadingDaily));
            // 创建一个成功的AjaxJson对象，并放入Page对象，以便于前端展示
            return AjaxJson.success(request, response).put("page", pageView);
        }
    }


    @PostMapping("/save")
    @Operation(summary = "保存表计记录任务", description = "新增或更改表计记录任务信息。")
    public AjaxJson save(@RequestBody HtMeterReadingDaily.HtMeterReadingDailySaveDto htMeterReadingDailySaveDto, HttpServletRequest request, HttpServletResponse response) {
        if (htMeterReadingDailySaveDto == null) {
            return AjaxJson.error("参数错误", HttpStatus.HTTP_BAD_REQUEST, request, response);
        }
        HtMeterReadingDaily htMeterReadingDaily = htMeterReadingDailySaveDto.toHtMeterReadingDaily();
        // 设置实体对象的更新者为当前用户
        htMeterReadingDaily.setUpdatedBy(new SysUser(TokenUtil.getUid(request)));
        // 调用服务层的save方法，尝试保存或更新实体对象，返回保存结果的Map
        htMeterReadingDailyService.save(htMeterReadingDaily);
        // 根据保存结果返回相应的AjaxJson对象
        return AjaxJson.success("保存成功", request, response).put("htMeterReadingDaily", htMeterReadingDailyService.get(htMeterReadingDaily.getId()).toHtMeterReadingDailyView());
    }

    @PostMapping("/batchSave")
    @Operation(summary = "批量保存表计记录任务", description = "批量保存表计记录任务信息。")
    public AjaxJson batchSave(@RequestBody List<HtMeterReadingDaily.HtMeterReadingDailyBatchSaveDto> htMeterReadingDailyBatchSaveDtoList, HttpServletRequest request, HttpServletResponse response) throws IOException, InterruptedException {
        if (htMeterReadingDailyBatchSaveDtoList == null) {
            return AjaxJson.error("参数错误", HttpStatus.HTTP_BAD_REQUEST, request, response);
        }
        List<HtMeterReadingDaily> htMeterReadingDailyList = htMeterReadingDailyBatchSaveDtoList.stream().map(HtMeterReadingDaily.HtMeterReadingDailyBatchSaveDto::toHtMeterReadingDaily).toList();
        SysUser createdBy = new SysUser(TokenUtil.getUid(request));
        LocalDate now = LocalDate.now();
        htMeterReadingDailyList.forEach(htMeterReadingDaily -> {
            htMeterReadingDaily.setUpdatedBy(createdBy);
            htMeterReadingDaily.setCreatedBy(createdBy);
            htMeterReadingDaily.setDate(now);
        });
        htMeterReadingDailyService.insertBatch(htMeterReadingDailyList);
        return AjaxJson.success("保存成功", request, response);
    }

    @PostMapping("/approval")
    @Operation(summary = "审批表计记录任务", description = "审批表计记录任务信息。")
    public AjaxJson approval(@RequestBody HtMeterReadingDaily.HtMeterReadingDailyApprovalSaveDto htMeterReadingDailyApprovalSaveDto, HttpServletRequest request, HttpServletResponse response){
        HtMeterReadingDaily htMeterReadingDaily = htMeterReadingDailyApprovalSaveDto.toHtMeterReadingDaily();
        htMeterReadingDaily.setUpdatedBy(new SysUser(TokenUtil.getUid(request)));
        htMeterReadingDailyService.approval(htMeterReadingDaily);
        return AjaxJson.success("审批成功", request, response);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除表计记录任务", description = "根据ID删除表计记录任务信息。", parameters = {@Parameter(name = "ids", description = "表计记录任务ID列表，多个ID用英文逗号分隔。")})
    public AjaxJson delete(@RequestParam String ids, HttpServletRequest request, HttpServletResponse response) {
        // 检查ID参数是否为空，为空则返回错误响应
        if (ids == null) {
            return AjaxJson.error("参数错误", HttpStatus.HTTP_BAD_REQUEST, request, response);
        }
        // 调用服务层方法，执行删除操作
        htMeterReadingDailyService.delete(ids);
        // 返回成功响应
        return AjaxJson.success(request, response);
    }
}
